package com.slg.android.manyanime.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.bean.Episode;
import com.slg.android.manyanime.callBackInterface.OnClickCallBack;

import java.util.ArrayList;

/**
 * Created by E540 on 2015/3/12.
 */
public class EpisodeAdapter extends RecyclerView.Adapter<EpisodeAdapter.EpisodeViewHolder> {
    private Context context;
    private ArrayList<Episode> data;
    private OnClickCallBack listener;

    public EpisodeAdapter(Context context, ArrayList<Episode> data) {
        this.context = context;
        this.data = data;
    }

    public void setListener(OnClickCallBack listener) {
        this.listener = listener;
    }

    @Override
    public EpisodeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_episode, parent, false);

        return new EpisodeViewHolder(view);
    }

    @Override
    public void onBindViewHolder(EpisodeViewHolder holder, int position) {
        holder.num.setText(String.valueOf(position + 1));
        holder.listener = this.listener;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class EpisodeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @ViewInject(R.id.item_episode_current_num)
        public TextView num;
        public OnClickCallBack listener;

        public EpisodeViewHolder(View itemView) {
            super(itemView);
            ViewUtils.inject(this, itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (this.listener != null) {
                listener.onItemClick(v, getPosition());
            }
        }
    }
}
